import { createSlice, Dispatch } from "@reduxjs/toolkit";
import axios from "axios";

const userStore = createSlice({
    name: 'user',
    // 初始化数据
    initialState: {
        userInfo: {}
    },
    // 定义修改数据方法
    reducers: {
        // 获取用户信息
        getUserInfo(state,action) {
            state.userInfo = action.payload
        }
    }
})

// actions解构
const { getUserInfo } = userStore.actions

// 异步请求
const fetchGetUserInfo = (dispatch: Dispatch) => {
    return async() => {
        const res =await axios.get('/地址')
        dispatch(getUserInfo(res.data))
    }
}

// 获取reducer函数
const userReducer = userStore.reducer

// 导出actions函数对象
export { getUserInfo, fetchGetUserInfo }

// 导出reducer函数
export default userReducer


